package com.zaoqi.service.impl;

import com.zaoqi.pojo.User;
import com.zaoqi.repository.UserRepository;
import com.zaoqi.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public User login(String username, String password) {
        return userRepository.findByUsernameAndPassword(username, password)
                .orElse(null); // 如果找不到则返回 null
    }

    @Override
    public void register(User user) {
        if (userRepository.findByUsername(user.getUsername()).isPresent()) {
            throw new RuntimeException("用户名已存在");
        }
        userRepository.save(user); // 保存用户信息
    }
}
